fontchooser: Set a fixed size on the preview cell
authorBenjamin Otte <otte@redhat.com>
Thu, 22 Sep 2011 16:00:37 +0000 (18:00 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 22 Sep 2011 19:44:05 +0000 (21:44 +0200)
This way, all rows have the same height. It has 3 advantages:
1) No actual computation is necessary to compute the size of the cell.
   This speeds up the list tremendously (filling out the list goes from
   25s to 3s).
2) Buggy fonts don't mess up the list anymore with their weird sizes.
   Instead, they are clipped / empty space is added.
3) Buggy fonts look more buggy. So their use is hopefully discouraged.

gtk/gtkfontchooserwidget.c

index ef0f525f8e24b5aee7a6c340309cb802093657e9..d7c3acc20978bc32d48ec24e6deac94e41c9d5fb 100644 (file)
@@ -78,6 +78,7 @@ struct _GtkFontChooserWidgetPrivate
 {
   GtkWidget    *search_entry;
   GtkWidget    *family_face_list;
+  GtkCellRenderer *family_face_cell;
   GtkWidget    *list_scrolled_window;
   GtkWidget    *empty_list;
   GtkWidget    *list_notebook;
@@ -826,9 +827,12 @@ gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget       *font
   attribute->end_index = first_line_len;
   pango_attr_list_insert (attrs, attribute);
 
-  attribute = pango_attr_font_desc_new (font_desc);
-  attribute->start_index = first_line_len;
-  pango_attr_list_insert (attrs, attribute);
+  if (font_desc)
+    {
+      attribute = pango_attr_font_desc_new (font_desc);
+      attribute->start_index = first_line_len;
+      pango_attr_list_insert (attrs, attribute);
+    }
 
   attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
   attribute->start_index = first_line_len;
@@ -872,14 +876,47 @@ gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
   g_free (text);
 }
 
+static void
+gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
+{
+  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
+  PangoAttrList *attrs;
+  GtkRequisition size;
+
+  gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
+
+  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, 
+                                                          NULL,
+                                                          1);
+  
+  g_object_set (priv->family_face_cell,
+                "attributes", attrs,
+                "text", "x\nx",
+                NULL);
+
+  pango_attr_list_unref (attrs);
+
+  gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
+                                        priv->family_face_list,
+                                        &size,
+                                        NULL);
+  gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
+}
+
 static void
 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
 {
   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
   GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
-  GtkCellRenderer *cell;
   GtkTreeViewColumn *col;
 
+  g_signal_connect_data (priv->family_face_list,
+                         "style-updated",
+                         G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
+                         fontchooser,
+                         NULL,
+                         G_CONNECT_AFTER | G_CONNECT_SWAPPED);
+
   priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
                                                     PANGO_TYPE_FONT_FAMILY,
                                                     PANGO_TYPE_FONT_FACE,
@@ -898,14 +935,14 @@ gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
   gtk_tree_view_set_rules_hint      (treeview, TRUE);
   gtk_tree_view_set_headers_visible (treeview, FALSE);
 
-  cell = gtk_cell_renderer_text_new ();
-  g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
+  priv->family_face_cell = gtk_cell_renderer_text_new ();
+  g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
 
   col = gtk_tree_view_column_new ();
   gtk_tree_view_column_set_title (col, _("Font Family"));
-  gtk_tree_view_column_pack_start (col, cell, TRUE);
+  gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
   gtk_tree_view_column_set_cell_data_func (col,
-                                           cell,
+                                           priv->family_face_cell,
                                            gtk_font_chooser_widget_cell_data_func,
                                            fontchooser,
                                            NULL);
@@ -913,6 +950,8 @@ gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
   gtk_tree_view_append_column (treeview, col);
 
   gtk_font_chooser_widget_load_fonts (fontchooser);
+
+  gtk_font_chooser_widget_set_cell_size (fontchooser);
 }
 
 static void